//
//  RegisterViewController.m
//  e-nurse
//
//  Created by  on 9/27/12.
//  Copyright (c) 2012 __MyCompanyName__. All rights reserved.
//

#import "RegisterViewController.h"
#import "ServerProxy.h"
#import "User.h"
#import "ProfileViewController.h"
#import "RegisterResult.h"
#import "ActivateUserViewController.h"
#import "RegisterLicenseViewController.h"
#import "ALToastView.h"

@interface RegisterViewController ()

- (void)registerCompleted:(RegisterResult*)registerResult;
- (void)registerCompletedImpl:(RegisterResult*)registerResult;

- (BOOL)checkInput;

@end

@implementation RegisterViewController
@synthesize userNameTextField = _userNameTextField;
@synthesize passwordTextField = _passwordTextField;
@synthesize phoneTextField = _phoneTextField;
@synthesize readLicenseSwitch = _readLicenseSwitch;
@synthesize activityIndicatorView = _activityIndicatorView;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.activityIndicatorView.hidden = true;
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    
    self.userNameTextField = nil;
    self.passwordTextField = nil;
    self.phoneTextField = nil;
    self.readLicenseSwitch = nil;
    self.activityIndicatorView = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return NO;
}

- (void)registerButtonClicked:(id)sender
{
    [self.view endEditing:YES];
    if(![self checkInput])
    {
        [ALToastView toastInView:self.view withText:NSLocalizedString(@"PROMPT_USERNAME_PASSWORD_PHONE_CANT_EMPTY", nil)];
        
        return;
    }
    
    if(!self.readLicenseSwitch.on)
    {
        [ALToastView toastInView:self.view withText:NSLocalizedString(@"PROMPT_READ_LICENSE", nil)];
        return;       
    }
    
    self.activityIndicatorView.hidden = false;
    [self.activityIndicatorView startAnimating];
    
    User* user = [[User alloc] init];
    user.name = self.userNameTextField.text;
    user.phone = self.phoneTextField.text;
    user.password = self.passwordTextField.text;
    [[ServerProxy sharedInstance] register:user completedAction:@selector(registerCompleted:) target:self];
}

- (void)registerCompleted:(RegisterResult*)registerResult
{
    [registerResult retain];
    [self performSelectorOnMainThread:@selector(registerCompletedImpl:) withObject:registerResult waitUntilDone:NO]; 
}

- (void)registerCompletedImpl:(RegisterResult*)registerResult
{
    [self.activityIndicatorView stopAnimating];
    self.activityIndicatorView.hidden = true;
    
    switch (registerResult.errorCode) {
        case 0:
        {
            ActivateUserViewController* activateUserViewController = [[ActivateUserViewController alloc] initWithNibName:@"ActivateUserViewController" bundle:nil];
            activateUserViewController.userName = registerResult.userName;
            activateUserViewController.userId = registerResult.userId;
            activateUserViewController.apiKey = registerResult.apiKey;
            [self.navigationController pushViewController:activateUserViewController animated:NO];
            
            [activateUserViewController release];
        }
            break;
        case 1:
            [ALToastView toastInView:self.view withText:NSLocalizedString(@"PROMPT_REGISTER_FAILED_USERNAME_EXISTING", nil)];
            break;
        case 2:
            [ALToastView toastInView:self.view withText:NSLocalizedString(@"PROMPT_REGISTER_FAILED_PHONE_EXISTING", nil)];
            break;
        default:
            break;
    }
    
    [registerResult release];
}

- (void)backButtonClicked:(id)sender
{
   [self.navigationController popViewControllerAnimated:YES]; 
}

- (void)registerLicenseButtonClicked:(id)sender
{
    RegisterLicenseViewController* registerLicenseVC = [[RegisterLicenseViewController alloc] initWithNibName:@"RegisterLicenseViewController" bundle:nil];
    
    [self presentModalViewController:registerLicenseVC animated:YES];
    
    [registerLicenseVC release];
}

- (BOOL)checkInput
{
    return ![self.userNameTextField.text isEqualToString:@""] && ![self.passwordTextField.text isEqualToString:@""] && ![self.phoneTextField.text isEqualToString:@""];
}

@end
